package com.ganzalang.gmall.designpatterns.factory.simplefactory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SimpleFactoryPatternDemo {

    public static void main(String[] args) {
        ProductFactory productFactory = new ProductFactory();
        Product productA = productFactory.create("A");
        Product productB = productFactory.create("B");
        productA.operation();
        productB.operation();
    }

    public interface Product {
        void operation();
    }

    public static class ProductA implements Product {
        @Override
        public void operation() {
            log.info("ProductA开始生产");
        }
    }

    public static class ProductB implements Product {
        @Override
        public void operation() {
            log.info("ProductB开始生产");
        }
    }

    public static class ProductFactory {
        public Product create(String type) {
            Product product = null;
            if ("A".equals(type)) {
                product = new ProductA();
                return product;
            }else if ("B".equals(type)) {
                product = new ProductB();
                return product;
            }
            return product;
        }
    }
}
